package com.tongzhan.controller;

import com.tongzhan.base.Return;
import com.tongzhan.entity.religion.ExemplaryCompany;
import com.tongzhan.entity.religion.Representatives;
import com.tongzhan.service.GraceService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

@Slf4j
@RestController
@RequestMapping("/grace")
public class GraceController {

    @Resource
    private GraceService graceService;

    /**
     * 新增代表人士接口
     *
     * @param name 姓名
     * @param age 年龄
     * @param sex 性别
     * @param company 公司
     * @param introduction 简介
     * @param grace 个人风采
     * @param picture 照片
     * @return Return
     */
    @PostMapping("/newRepresentatives")
    public Return newRepresentatives(String name, Integer age, String sex, String company, String introduction,
                                     String grace, String picture) {
        if (StringUtils.isEmpty(name) || "".equals(name.trim()) || age == null
                || StringUtils.isEmpty(sex) || StringUtils.isEmpty(company) || "".equals(company.trim())
                || StringUtils.isEmpty(introduction) || "".equals(introduction.trim())
                || StringUtils.isEmpty(grace) || "".equals(grace.trim()) || StringUtils.isEmpty(picture)) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        if (age.toString().length() > 5) {
            return new Return(Return.FAIL, "年龄过大");
        }
        if (company.trim().length() > 100) {
            return new Return(Return.FAIL, "所属单位过长");
        }
        if (introduction.trim().length() > 500) {
            return new Return(Return.FAIL, "简介过长");
        }
        if (grace.trim().length() > 2000) {
            return new Return(Return.FAIL, "个人风采过长");
        }
        Representatives representatives = new Representatives();
        representatives.setName(name.trim());
        representatives.setAge(age);
        representatives.setSex(sex);
        representatives.setCompany(company.trim());
        representatives.setIntroduction(introduction.trim());
        representatives.setGrace(grace.trim());
        representatives.setPicture(picture.trim());
        return graceService.newRepresentatives(representatives);
    }

    /**
     * 获取代表人士列表接口
     *
     * @param page 页
     * @param size 大小
     * @return Return
     */
    @PostMapping("/getRepresentativesList")
    public Return getRepresentativesList(Integer page, @RequestParam(required = false, defaultValue = "10") Integer size) {
        if (page == null) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        if (page <= 0) {
            page = 1;
        }
        return graceService.getRepresentativesList(page, size);
    }

    /**
     * 代表人士详情接口
     *
     * @param representCode 代表人士编码
     * @return Return
     */
    @PostMapping("/representativesDetails")
    public Return representativesDetails(String representCode) {
        if (StringUtils.isEmpty(representCode)) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        return graceService.representativesDetails(representCode);
    }

    /**
     * 新增进步示范单位接口
     *
     * @param companyName 单位名称
     * @param address 单位地址
     * @param typeCode 类型编码
     * @param number 单位人数
     * @param buildTime 成立时间
     * @param grace 单位风采
     * @param picture 图片
     * @return Return
     */
    @PostMapping("/newExemplaryCompany")
    public Return newExemplaryCompany(String companyName, String address, String typeCode, Long number, String buildTime,
                                      String grace, String picture) {
        if (StringUtils.isEmpty(companyName) || "".equals(companyName.trim()) || StringUtils.isEmpty(address) || "".equals(address.trim())
                || StringUtils.isEmpty(buildTime) || StringUtils.isEmpty(grace) || "".equals(grace.trim())
                || StringUtils.isEmpty(typeCode) || StringUtils.isEmpty(picture) || number == null) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        if (companyName.trim().length() > 100) {
            return new Return(Return.FAIL, "单位名称过长");
        }
        if (address.trim().length() > 200) {
            return new Return(Return.FAIL, "单位地址过长");
        }
        if (grace.trim().length() > 300) {
            return new Return(Return.FAIL, "单位风采过长");
        }
        ExemplaryCompany exemplaryCompany = new ExemplaryCompany();
        try {
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            exemplaryCompany.setBuildTime(LocalDate.parse(buildTime, dtf));
        } catch (Exception e) {
            return new Return(Return.FAIL, "日期格式不正确，为 2020-01-01");
        }
        exemplaryCompany.setCompanyName(companyName.trim());
        exemplaryCompany.setAddress(address.trim());
        exemplaryCompany.setTypeCode(typeCode);
        exemplaryCompany.setNumber(number);
        exemplaryCompany.setGrace(grace.trim());
        exemplaryCompany.setPicture(picture);
        return graceService.newExemplaryCompany(exemplaryCompany);
    }

    /**
     * 获取进步示范代为列表接口
     *
     * @param page 页
     * @param size 大小
     * @return Return
     */
    @PostMapping("/getExemplaryCompanyList")
    public Return getExemplaryCompanyList(Integer page, @RequestParam(required = false, defaultValue = "10") Integer size) {
        if (page == null) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        if (page <= 0) {
            page = 1;
        }
        return graceService.getExemplaryCompanyList(page, size);
    }

    /**
     * 进步示范单位详情
     *
     * @param companyCode 单位编码
     * @return Return
     */
    @PostMapping("/exemplaryCompanyDetails")
    public Return exemplaryCompanyDetails(String companyCode) {
        if (StringUtils.isEmpty(companyCode)) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        return graceService.exemplaryCompanyDetails(companyCode);
    }

    /**
     * 新增/修改单位类型接口
     *
     * @param typeCode 单位类型编码
     * @param typeName 单位类型名称
     * @return Return
     */
    @PostMapping("/editCompanyType")
    public Return editCompanyType(String typeCode, String typeName) {
        if (StringUtils.isEmpty(typeName) || "".equals(typeName.trim())) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        if (typeName.trim().length() > 50) {
            return new Return(Return.FAIL, "类型名称过长");
        }
        return graceService.editCompanyType(typeCode, typeName.trim());
    }

    /**
     * 获取单位类型列表接口
     *
     * @return Return
     */
    @PostMapping("/getCompanyTypeList")
    public Return getCompanyTypeList() {
        return graceService.getCompanyTypeList();
    }

    /**
     * 删除单位类型接口
     *
     * @param typeCode 类型编码
     * @return Return
     */
    @PostMapping("/deleteCompanyType")
    public Return deleteCompanyType(String typeCode) {
        if (StringUtils.isEmpty(typeCode)) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        return graceService.deleteCompanyType(typeCode);
    }
}
